package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.List;

import com.pesking.core.SpringBeanUtils;
import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.sysparam.SystemParameterManage;
import com.pesking.core.util.StringUtils;

/**
 * 在SQL语句中借助此参数可以让表名指向不同的数据库名
 * @author admin
 *
 */
public class SchemaSqlParameterHandler extends SqlParameterProcessor {
	@Override
	public String getTitle() {
		return "Schema参数";
	}

	@Override
	public String getUsage() {
		return super.getUsage();
	}

	@Override
	public String getDescription() {
		return null;
	}

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String schema = sqlParameter.getField(2);
		if (StringUtils.isBlank(schema)){
			String name = sqlParameter.getName();
			if (!StringUtils.isBlank(name)){
				schema = parameter.getParameter(name);
			}
			if (StringUtils.isBlank(schema)){
				schema = SystemParameterManage.getString(name);
			}
		}
		if (!StringUtils.isBlank(schema)){
			String schemaValue = SpringBeanUtils.getEnvirnmentProperty("pesking." + schema);
			if (!StringUtils.isBlank(schemaValue)){
				schemaValue = schemaValue.trim() + ".";
				sbSQL.append(schemaValue);
			}
		}
	}

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"SCHEMA"});
	}

}
